# -*- coding: utf-8 -*-
import sys,serial,json,time,redis,traceback

flog=open("/home/wwwroot/pavgps/shake.log",'w')
try:
    rd = redis.Redis(host='127.0.0.1',port=6379)
    conffile = open('/home/wwwroot/pavgps/config.json','r')
    confstr = conffile.read()
    conf = json.loads(confstr)
    ser = serial.Serial(conf["ShakeSerialPort"], 9600, timeout=0.5)
except Exception as e:
    print("============")
    errstr=traceback.print_exc()
    flog.write(""+repr(e));
    flog.close()

def getcode(bstr):
    for k,v in enumerate(bstr):
        if v==2:
            return k
    return 0


print("start...")
while(1):
    line = ser.readline()
    if len(line)<4:
        continue

    shake = getcode(line)
    print(shake)
    rd.set("shake",shake)
